package com.gxh.controller.admin;


import com.gxh.dto.SwiperDTO;
import com.gxh.dto.SwiperPageQueryDTO;
import com.gxh.entity.Swiper;
import com.gxh.result.PageResult;
import com.gxh.result.Result;
import com.gxh.service.SwiperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/swiper")
@Api(tags = "轮播图管理")
@Slf4j
public class SwiperController {

    @Autowired
    private SwiperService swiperService;

    /**
     * 轮播图分页查询
     * @param swiperPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("轮播图分页查询")
    public Result<PageResult> page(SwiperPageQueryDTO swiperPageQueryDTO){
        log.info("分页查询轮播图{}",swiperPageQueryDTO);
        PageResult pageResult = swiperService.pageQuery(swiperPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增轮播图
     * @param swiperDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增轮播图")
    @CacheEvict(value = "swiperCache",allEntries = true)
    public Result save(@RequestBody SwiperDTO swiperDTO){
        log.info("新增轮播图{}",swiperDTO);
        swiperService.save(swiperDTO);
        return Result.success();
    }

    /**
     * 修改轮播图
     * @param swiperDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改轮播图")
    @CacheEvict(value = "swiperCache",allEntries = true)
    public Result update(@RequestBody SwiperDTO swiperDTO){
        log.info("修改轮播图{}",swiperDTO);
        swiperService.update(swiperDTO);
        return Result.success();
    }

    /**
     * 根据id查询轮播图
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询轮播图")
    public Result<Swiper> getById(@PathVariable Long id){
        log.info("根据id查询轮播图{}",id);
        Swiper swiper = swiperService.getById(id);
        return Result.success(swiper);
    }

    /**
     * 批量删除轮播图
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除轮播图")
    @CacheEvict(value = "swiperCache",allEntries = true)
    public Result delete(@RequestBody List<Long> ids){
        log.info("批量删除轮播图{}",ids);
        swiperService.deleteBatch(ids);
        return Result.success();
    }
}
